<template>
	<view class="property">
		<view class="id">
			<view class="id-box">
				<span>身份证</span><span style="margin-left: 20rpx;">130282819948291922</span>
			</view>
			<view class="id-upload">
				<u-upload
					:fileList="fileList1"
					@afterRead="afterRead"
					name="1"
					multiple
					:maxCount="1"
					accept="image"
					@delete="deletePic"
					width="330rpx"
					height="200rpx"
				>
				</u-upload>
				<u-upload
					:fileList="fileList2"
					@afterRead="afterRead"
					 @delete="deletePic"
					name="2"
					multiple
					:maxCount="1"
					accept="image"
					width="330rpx"
					height="200rpx"
				>
				</u-upload>
			</view>
		</view>
		<view class="health">
			<view class="health-box">
				<span>健康证</span>
			</view>
			<view class="health-upload">
				<u-upload
					:fileList="fileList3"
					@afterRead="afterRead"
					@delete="deletePic"
					name="3"
					multiple
					:maxCount="1"
					accept="image"
					width="675rpx"
					height="400rpx"
				>
				</u-upload>
			</view>
		</view>
	</view>
</template>

<script>
	import { getByUserId, barRiderUpdate } from '@/api/user.js'
	import { environment } from '@/config/environment.js'
	export default {
		data() {
			return {
				resourcesUrl: environment.baseURL,
				baseUrlImg: environment.baseUrlImg,
				detailObj: {},
				fileList1: [],
				fileList2: [],
				fileList3: [],
			}
		},
		onLoad() {
			getByUserId().then(res => {
				this.detailObj = res.data
				let frontImg = this.baseUrlImg + res.data.frontImg
				let behindImg = this.baseUrlImg + res.data.behindImg
				let healthyImg = this.baseUrlImg + res.data.healthyImg
				this.fileList1.push({url: frontImg})
				this.fileList2.push({url: behindImg})
				this.fileList3.push({url: healthyImg})
			})
		},
		methods: {
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url, `fileList${event.name}`)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url, imglist) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: this.resourcesUrl + 'gscloud_biz/oss/upload_minio', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						header: {
							'Authorization': 'NjFlODBlMTIzMDVhNGMzMzliOThmN2Y0MTZmMWFkMzUxNzI0MDI4NjM0MDE2Mg=='
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
							if (imglist == "fileList1") {
								this.detailObj.frontImg = JSON.parse(res.data).data.dir + JSON.parse(res.data).data.fileName
							}
							if (imglist == "fileList2") {
								this.detailObj.behindImg = JSON.parse(res.data).data.dir + JSON.parse(res.data).data.fileName
							}
							if (imglist == "fileList3") {
								this.detailObj.healthyImg = JSON.parse(res.data).data.dir + JSON.parse(res.data).data.fileName
							}
							uni.hideLoading();
							barRiderUpdate(this.detailObj)
						}
					});
				})
			},
			deletePic(event) {
				if (event.name == 1) {
					this[`fileList${event.name}`].splice(event.index, 1)
					this.detailObj.frontImg = ''
				}
				if (event.name == 2) {
					this[`fileList${event.name}`].splice(event.index, 1)
					this.detailObj.behindImg = ''
				}
				if (event.name == 3) {
					this[`fileList${event.name}`].splice(event.index, 1)
					this.detailObj.healthyImg = ''
				}
			},
		},
	}
</script>

<style lang="scss" scoped>
	.property{
		height: 97vh;
		padding: 20rpx;
		background-color: #F7F8FA;
		font-size: 26rpx;
		.id{
			padding: 20rpx;
			background-color: #fff;
			.id-upload{
				margin: 20rpx 0;
				display: flex;
				justify-content: space-between;
			}
		}
		.health{
			padding: 20rpx;
			margin: 20rpx 0;
			background-color: #fff;
			.health-upload{
				margin: 20rpx 0;
			}
		}
	}
</style>